package ui.map {

	import common.loader.Info;
	import common.loader.LoaderManager;
	import common.loader.LoaderType;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	import flash.utils.setInterval;
	
	import globals.Global;
	import globals.Setting;
	import globals.State;
	
	import ui.panel.PanelType;
	
	import util.DisplayUtil;
	
	public class Arena extends Sprite {
		
		private var mapLayer:Sprite;
		private var itemLayer:Sprite;
		private var gridLayer:Sprite;
		
		private var mapList:Dictionary;
		
		private var pathList:Array;
		private var buildList:Array;
		private var itemList:Array;
		
		public static const BUIDL_GRID:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("tower_grid", "ui"));

		public function Arena() {
			super();
			init();
		}
		
		private function init():void {
			mapLayer = new Sprite();
			itemLayer = new Sprite();
			gridLayer = new Sprite();
			addChild(mapLayer);
			addChild(itemLayer);
			addChild(gridLayer);
			
			mapList = new Dictionary();
			var map:Bitmap;
			for (var i:int = 0; i<Setting.TILE_ROW; i++) {
				for (var j:int = 0; j<Setting.TILE_COL; j++) {
					map = new Bitmap();
					map.x = Setting.TILE_WIDTH * j;
					map.y = Setting.TILE_HEIGHT * i;
					mapLayer.addChild(map);
					mapList[i + "_" + j] = map;
				}
			}
			
			pathList = new Array();
			buildList = new Array();
			itemList = new Array();
		}
		
		public function update(tileData:Array):void {
			var map:Bitmap;
			var bmd:BitmapData;
			for (var i:int = 0; i<tileData.length; i++) {
				for (var j:int = 0; j<tileData[i].length; j++) {
					map = mapList[i + "_" + j];
					map.bitmapData = Global.assetsManager.getAssetsAsBitmapData(tileData[i][j], "map");
				}
			}
			
			LoaderManager.add("resources/map/path1.png", LoaderType.IMAGE, pathLoadHandler);
			
			addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		public function updateItems(itemData:Array):void {
			itemList = itemData;
			var item:Bitmap;
			var data:Array;
			for (var i:int = 0; i<itemData.length; i++) {
				data = itemData[i];
				item = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(data[0], "map"));
				item.x = data[1] * 40;
				item.y = data[2] * 40 - item.height + 20;
				itemLayer.addChild(item);
			}
		}
		
		private function pathLoadHandler(bitmapData:BitmapData):void {
			var pathBitmap:Bitmap = new Bitmap(bitmapData);
			mapLayer.addChild(pathBitmap);
		}
		
		private function clickHandler(event:MouseEvent):void {
//			var point:Point = new Point((int(event.stageX / Setting.TILE_WIDTH) + 1)* Setting.TILE_WIDTH, 
//				(int(event.stageY / Setting.TILE_HEIGHT) + 0) * Setting.TILE_HEIGHT);
//			Global.uiManager.optionPanel.openByPoint(point);
			var point:Point = new Point(int(event.stageX / Setting.TILE_WIDTH), 
				int(event.stageY / Setting.TILE_HEIGHT));
			var state:uint = Global.stateManager.getState();
			if (state == State.STATE_BUILD) {
				var list:Array = Global.mapManager.getBuildList();
				for (var i:int = 0; i<list.length; i++) {
					if (list[i][0] == point.x && list[i][1] == point.y) {
						buildTower(point.x, point.y);
						hideBuildGrid();
						Global.stateManager.enterState(State.STATE_NORMAL);
						return;
					}
				}
				Global.uiManager.exitBuildState();
			}
		}
		
		public function showBuildGrid():void {
			var list:Array = Global.mapManager.getBuildList();
			var g:Graphics = gridLayer.graphics;
			g.clear();
			g.beginFill(0x000000, 0.3);
			for (var i:int = 0; i<list.length; i++) {
				g.drawRect(list[i][0] * 38, list[i][1] * 38, 37, 37);
			}
			g.endFill();
			
			addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
		}
		
		public function hideBuildGrid():void {
			gridLayer.graphics.clear();
			removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			DisplayUtil.removeChild(this, BUIDL_GRID);
		}
		
		private function buildTower(tx:uint, ty:uint):void {
			Global.unitManager.addTowerById("tower1", tx, ty);
		}
		
		private function moveHandler(event:MouseEvent):void {
			var mx:int = event.localX / Setting.TILE_WIDTH;
			var my:int = event.localY / Setting.TILE_HEIGHT;
			var list:Array = Global.mapManager.getBuildList();
			var found:Boolean = false;
			for (var i:int = 0; i<list.length; i++) {
				if (mx == list[i][0] && my == list[i][1]) {
					BUIDL_GRID.x = mx * Setting.TILE_WIDTH - 3;
					BUIDL_GRID.y = my * Setting.TILE_HEIGHT - 3;
					addChild(BUIDL_GRID);
					found = true;
					break;
				}
			}
			
			if (!found) {
				DisplayUtil.removeChild(this, BUIDL_GRID);
			}
		}
		
		public function clear():void {
			var map:Bitmap;
			for (var i:int = 0; i<Setting.TILE_ROW; i++) {
				for (var j:int = 0; j<Setting.TILE_COL; j++) {
					map = mapList[i + "_" + j];
					map.bitmapData = null;
				}
			}
			
			removeEventListener(MouseEvent.CLICK, clickHandler);
		}
	}
}